package org.tarena.dang.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.search.AddressTerm;

import org.tarena.dang.dao.OrderDAO;
import org.tarena.dang.entity.CartItem;
import org.tarena.dang.entity.UserAdress;
import org.tarena.dang.util.DbUtil;

import com.opensymphony.xwork2.Result;

public class OrderDAOImpl implements OrderDAO {

	@Override
	public boolean insertAdd(int id, UserAdress adress) throws SQLException {
		String sql = "insert into d_receive_address(user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?)";
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		pst.setInt(1, id);
		pst.setString(2, adress.getReceiveName());
		pst.setString(3, adress.getFullAddress());
		pst.setString(4, adress.getPostalCode());
		pst.setString(5, adress.getMobile());
		pst.setString(6, adress.getPhone());
		int n = pst.executeUpdate();
		if(n==1){
			return true;
		}
		return false;
	}

	@Override
	public int saveItems(int id, CartItem c) throws SQLException {
		String sql = "insert into d_item(order_id,product_id,product_name,dang_price,product_num,amount) values(?,?,?,?,?,?)";
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		pst.setInt(1, id);
		pst.setInt(2, c.getPro().getId());
		pst.setString(3, c.getPro().getProductName());
		pst.setDouble(4, c.getPro().getDangPrice());
		pst.setInt(5, c.getQty());
		pst.setDouble(6, c.getQty()*c.getPro().getDangPrice());
		int n =pst.executeUpdate();
		System.out.println("插入了几件商品=="+n);
		return n;
	}

	@Override
	public int saveOrder(int id, UserAdress address, double count,
			int waitPay, long currentTimeMillis) throws SQLException {
		String sql = "insert into d_order(user_id,status,order_time,total_price,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?,?,?)";
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
		pst.setInt(1, id);
		pst.setInt(2, waitPay);
		pst.setLong(3, currentTimeMillis);
		pst.setDouble(4, count);
		pst.setString(5, address.getReceiveName());
		pst.setString(6, address.getFullAddress());
		pst.setString(7, address.getPostalCode());
		pst.setString(8, address.getMobile());
		pst.setString(9, address.getPhone());
		int n = pst.executeUpdate();
		ResultSet re =  pst.getGeneratedKeys();
		re.next();
		return re.getInt(1);
	}

	@Override
	public List<UserAdress> getAddress(int id) throws SQLException {
		String sql = "select * from d_receive_address where user_id=?";
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		pst.setInt(1, id);
		ResultSet re = pst.executeQuery();
		List<UserAdress> list = new ArrayList<UserAdress>();
		
		while(re.next()){
			UserAdress address = new UserAdress();
			address.setId(re.getInt("id"));
			address.setFullAddress(re.getString("full_address"));
			System.out.println("*&&&**"+address.getFullAddress());
			address.setMobile(re.getString("mobile"));
			address.setPhone(re.getString("phone"));
			address.setPostalCode(re.getString("postal_code"));
			address.setReceiveName(re.getString("receive_name"));
			address.setUserId(id);
			list.add(address);
		}
		System.out.println("DAO get address execute");
		
		return list;
	}

	@Override
	public UserAdress getAddById(int addId) throws SQLException {
		String sql = "select * from d_receive_address where id=?";
		PreparedStatement pst = DbUtil.openConnection().prepareStatement(sql);
		pst.setInt(1, addId);
		ResultSet re = pst.executeQuery();
		UserAdress address = new UserAdress();
		while(re.next()){
			address.setId(re.getInt("id"));
			address.setFullAddress(re.getString("full_address"));
			address.setMobile(re.getString("mobile"));
			address.setPhone(re.getString("phone"));
			address.setPostalCode(re.getString("postal_code"));
			address.setReceiveName(re.getString("receive_name"));
						
		}
		System.out.println("DAO get address execute ok=="+address.getReceiveName());
		
		return address;
		
	}

}
